package com.ukao.steward.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.zxing.Result;
import com.ukao.steward.R;
import com.ukao.steward.base.BaseActivity;
import com.ukao.steward.bean.ScanCodeBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.ui.logistics.LeverFactory.SureBatchActivity;
import com.ukao.steward.util.L;
import com.ukao.steward.util.T;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
import rx.Observable;


public class ScanQRcodeActivity extends BaseActivity implements ZXingScannerView.ResultHandler {

   // @BindView(R.id.zxingview)
  //  ZXingView mQRCodeView;
    @BindView(R.id.title)
    TextView title;

    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(R.id.ZXingScannerView)
   ZXingScannerView mScannerView;
    private int type;
    private boolean isbarCode;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_qrcode);
        ButterKnife.bind(this);
        initView();

    }



    @Override
    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();          // Start camera on resume
    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();           // Stop camera on pause

    }


    @Override
    protected void initView() {
        title.setText("二维码/条码");
        type = getIntent().getIntExtra(Constant.ARG_PARAM1, 0); /*1是入厂扫码 2出厂  3是只要二维码*/
        isbarCode = getIntent().getBooleanExtra(Constant.ARG_PARAM2, false);

        titleLayout.setBackgroundColor(resources.getColor(R.color.left_head));
        if (isbarCode) {/*扫码订单*/
            //  mQRCodeView.changeToScanBarcodeStyle();
        }
    }


    @Override
    public void handleResult(Result result) {
        mScannerView.resumeCameraPreview(this);
        switch (type) {
            case 3:  /*只要二维码*/
                Intent mIntent = new Intent();
                mIntent.putExtra(Constant.ARG_PARAM1, result.getText().trim());
                setResult(RESULT_OK, mIntent);
                finish();
                break;

        }
    }


    public void scanCode(String result) {
        showProgressDialog();
        Observable observable = null;
        HashMap mHashMap = new HashMap<String, String>();
        if (type == 4) {
            mHashMap.put("orderNo", result);
        } else {
            mHashMap.put("code", result);
        }
        mHashMap.put("accessToken", SaveParams.getToken());
        switch (type) {
            case 1:
                observable = apiStores().scanCode(Constant.createParameter(mHashMap));

                break;
            case 2:
                observable = apiStores().batchOutscanCode(Constant.createParameter(mHashMap));
                break;

            case 4:
                observable = apiStores().OutOrdidsCanCode(Constant.createParameter(mHashMap));
                break;

        }

        addSubscription(observable,
                new ApiCallback<ScanCodeBean>() {
                    @Override
                    public void onSuccess(ScanCodeBean model) {

                        if (model.getHttpCode() == 200) {
                            L.i("ScanCodeBean=" + model.getData().toString());
                            Intent mIntent = new Intent(ScanQRcodeActivity.this, SureBatchActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putInt(Constant.ARG_PARAM2, type);
                            bundle.putParcelable(Constant.ARG_PARAM1, model.getData());
                            mIntent.putExtras(bundle);
                            startActivity(mIntent);
                            finish();

                        } else {
                            T.show(model.getMsg());
                        }
                    }


                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }
                });
    }



    @OnClick({R.id.back_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
        }
    }

}
